from typing import Annotated
from fastapi import Depends
from sqlmodel import SQLModel, Session, create_engine

from config.DBConfig import  MYSQL_HOST, MYSQL_DB_NAME, MYSQL_USER, MYSQL_PASS

# 导入所有数据库表


engine = create_engine(
    f"mysql+pymysql://{MYSQL_USER}:{MYSQL_PASS}@{MYSQL_HOST}/{MYSQL_DB_NAME}?charset=utf8mb4", echo=True
)


def create_db_and_tables():
    """创建数据库和所有表"""
    SQLModel.metadata.create_all(engine)


def get_session():
    """依赖注入，将engine注册到FastApi session中"""
    with Session(engine) as session:
        yield session


SessionDep = Annotated[Session, Depends(get_session)]
